package br.com.utfpr.menu;

import java.util.List;

import br.com.utfpr.controller.PessoaController;
import br.com.utfpr.data.Pessoa;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class EditarEscolheActivity extends ListActivity{
	
	private PessoaController pessoaController;	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.editar_escolhe);
        pessoaController = new PessoaController((Context) this);
    	atualizaLista();
    }   
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.layout.menu_consulta, menu);
        return true;
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Pessoa p = (Pessoa)l.getItemAtPosition(position);
		Intent intent = new Intent(this,EditarAlteraActivity.class);
		intent.putExtra("Pessoa", p);
		startActivity(intent);           
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.consulta_voltar:
            	startActivity(new Intent(this,MainActivity.class));
            break;
        }
        return true;
    }
    
    public void atualizaLista(){        
//      ArrayList<Pessoa> items = Pessoa.getAll(getApplicationContext());
      List<Pessoa> items = pessoaController.consultar();
      setListAdapter(new ArrayAdapter<Pessoa>(this, R.layout.lista_itens, R.id.item_nome, items)); 

    }

}
